package com.soyorin.pack.controller;

import com.soyorin.pack.entity.Pack;
import com.soyorin.pack.entity.PackItem;
import com.soyorin.pack.service.PackItemService;
import com.soyorin.pack.service.PackService;
import org.springframework.http.ResponseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pack")
public class PackController {

    @Autowired
    private PackService baseService;

    @Autowired
    private PackItemService packItemService;

    // 标准接口一: select-one
    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<Pack> selectById(@PathVariable Long id) {
        Pack Pack = baseService.getById(id);
        if(Pack != null) { // return1: 成功查询到数据
            // 计算总价格
            QueryWrapper<PackItem> wrapper = new QueryWrapper<>();
            wrapper.eq("rela_pack_id", id);
            List<PackItem> list = packItemService.list(wrapper);
            int sum = 0;
            for(PackItem packItem : list) {
                sum += packItem.getCount()*packItem.getPrice();
                Pack.setSumPrice(sum);
                baseService.updateById(Pack);
            }
            return ResponseEntity.status(200).body(Pack);
        } else { // return2: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }

    // 标准接口二: select-all
    @GetMapping
    @ResponseBody
    public PageInfo<Pack> selectAll(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Pack> list = baseService.list();
        return new PageInfo<>(list);
    }

    // 标准接口三: insert
    @PostMapping
    @ResponseBody
    public ResponseEntity<Pack> insert(@RequestBody Pack Pack) {
        boolean result = baseService.save(Pack);
        if(result) { // return1: 成功插入数据
            return ResponseEntity.status(200).body(Pack);
        } else { // return2: 插入数据失败
            return ResponseEntity.status(500).body(null);
        }
    }

    // 标准接口四: delete
    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseEntity<Pack> delete(@PathVariable Long id) {
        Pack Pack = baseService.getById(id);
        if(Pack != null) {
            boolean result = baseService.removeById(id);
            if(result) { // return1: 成功删除数据
                return ResponseEntity.status(200).body(Pack);
            } else { // return2: 删除数据失败
                return ResponseEntity.status(500).body(Pack);
            }
        } else { // return3: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }

    // 标准接口五: update
    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<Pack> update(
            @PathVariable Long id,
            @RequestBody Pack Pack) {
        Pack.setId(id);
        boolean result = baseService.updateById(Pack);
        Pack updatePack = baseService.getById(Pack.getId());
        if(result && updatePack != null) { // return1: 成功更新数据
            return ResponseEntity.status(200).body(updatePack);
        } else { // return2: 更新数据失败
            return ResponseEntity.status(500).body(Pack);
        }
    }

    // 标准接口六: select-eq
    @PostMapping("/select-eq")
    @ResponseBody
    public ResponseEntity<List<Pack>> selectEq(
            @RequestBody Map<String,Object> conditions) {
        QueryWrapper<Pack> wrapper = new QueryWrapper<>();
        for(Map.Entry<String, Object> entry : conditions.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            wrapper.eq(key, value);
        }
        List<Pack> list = baseService.list(wrapper);
        if(list != null) { // return1: 成功查询到数据
            return ResponseEntity.status(200).body(list);
        } else { // return2: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }

}